﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{

    public partial class frmPhieuNhapHang : Form
    {
        private DataTable _dtbDSH;
        private string _user;
        public frmPhieuNhapHang()
        { }
        public frmPhieuNhapHang(string username)
        {
            InitializeComponent();
            loadCB();
            _dtbDSH = KetNoi.GetTable("select top 0 MaSP,TenSP from tblSanPham");
            _user = username;
            lblNhanVien.Text = username;
        }

        private void frmPhieuNhapHang_Load(object sender, EventArgs e)
        {
            var col1 = new DataGridViewTextBoxColumn();
            var col2 = new DataGridViewTextBoxColumn();
            var col3 = new DataGridViewTextBoxColumn();
            var col4 = new DataGridViewTextBoxColumn();
            var col5 = new DataGridViewTextBoxColumn();

            col1.Name = "MaSP";
            col2.Name = "TenSP";
            col3.Name = "DonViTinh";
            col4.Name = "SoLuong";
            col5.Name = "DonGiaNhap";

            col1.HeaderText = "Mã Sản Phẩm";
            col2.HeaderText = "Tên sản phẩm";
            col3.HeaderText = "Đơn vị tính";
            col4.HeaderText = "Số lượng";
            col5.HeaderText = "Đơn giá nhập";

            dgvDSHangNhap.Columns.Add(col1);
            dgvDSHangNhap.Columns.Add(col2);
            dgvDSHangNhap.Columns.Add(col3);
            dgvDSHangNhap.Columns.Add(col4);
            dgvDSHangNhap.Columns.Add(col5);
        }

        private void loadCB()
        {
            DataTable tblNhaCC = KetNoi.GetTable("select*from tblNhaCungCap");
            cbxNhaCungCap.DataSource = tblNhaCC;
            cbxNhaCungCap.ValueMember = tblNhaCC.Columns["MaNhaCC"].ToString();
            cbxNhaCungCap.DisplayMember = tblNhaCC.Columns["TenNhaCC"].ToString();
            cbxNhaCungCap.Text = "____________________________";

            DataTable tblKhoSP = KetNoi.GetTable("select*from tblKho");
            cbxKhoHang.DataSource = tblKhoSP;
            cbxKhoHang.ValueMember = tblKhoSP.Columns["MaKho"].ToString();
            cbxKhoHang.DisplayMember = tblKhoSP.Columns["TenKho"].ToString();
            cbxKhoHang.Text = "____________________________";
        }

        private void btnDSHang_Click(object sender, EventArgs e)
        {
            frmDSHang frmHang = new frmDSHang();
            frmHang.ShowDialog();
            txtMaHang.Text = frmHang._MaHang;
        }

        private void btnThemHang_Click(object sender, EventArgs e)
        {
            if((txtMaHang.Text==null)||(txtMaHang.Text==""))
            {
                MessageBox.Show("Chưa chọn mã sản phẩm");
            }
            else
            {
                DataTable _dtb = KetNoi.GetTable("select MaSP,TenSP,DonViTinh from tblSanPham where MaSP='" + txtMaHang.Text + "'");
                string[] _rows;
                _rows = new string[]{
                    _dtb.Rows[0]["MaSP"].ToString(),
                    _dtb.Rows[0]["TenSP"].ToString(),
                    _dtb.Rows[0]["DonViTinh"].ToString(),
                    txtSoLuong.Text,
                    txtDonGiaNhap.Text
                };
                dgvDSHangNhap.Rows.Add(_rows);
                txtMaHang.Text = "";
            }
        }
    }
}
